<?php

namespace app\admin\controller;


use app\models\CompanyModel;
use app\models\HolidayModel;
use app\models\UserModel;

class Holiday extends Common
{
    /**
     * 用户管理模型.
     *
     *  @var UserModel
     */
    protected $userModel;

    /**
     * 节假日管理模型.
     *
     * @var HolidayModel
     */
    protected $holidayModel;

	/**
	 * 集团管理模型.
	 *
	 * @var CompanyModel
	 */
	protected $companyModel;

    /**
     * 节假日管理管理.
     *
     * @throws \think\exception\DbException
     *
     * @return mixed
     */
    public function lists()
    {
        if ($this->request->isAjax()) {
            $param = $this->request->param();
            $res = $this->holidayModel->getHolidayLists($param);
            $this->success('success', '', $res, 0);
        }

		return $this->fetch('',
			[
				'type'=>get_current_type(),
				'company_id'=>get_current_company_id()
			]
		);
    }

	/**
	 * 选择集团
	 * modify by Small Y
	 *
	 * @return mixed
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
	public function company()
	{
		$where = [];
		$where['status'] = 1;
        /*************过滤掉未分配系统账号的集团*************/
        $user_result = $this->userModel->where(['wtype' => 2])->select();
        $company_user_arr = [];
        foreach ($user_result as $key => $value){
            $company_user_arr[] = $value['company_id'];
        }
        $company_user_arr = array_unique($company_user_arr);
        $company_user_str = implode(',',$company_user_arr);
        $where['id'] = ['in',$company_user_str];

		$company = $this->companyModel->where($where)->select();
		return $this->fetch('', [
			'company' => $company,
			'date' => input('date')
		]);
	}

    /**
     * 删除假日数据
     *
     * @return mixed
     */
    public function date_del()
    {
        if ($this->request->isAjax() && $this->request->isPost()) {

            if (!$this->holidayModel->where(['id'=>input('id')])->delete()) {
                $this->error('删除数据失败', null, '', 1);
            }
            $this->success('删除数据成功', url('Holiday/lists'), '', 1);
        }
    }

    /**
     * 编辑节假日名称.
     *
     * @return mixed
     */
    public function holiday_edit()
    {
        if ($this->request->isAjax() && $this->request->isPost()) {
            $data = input('post.');
			$data['company_id'] = get_current_company_id();
            if (!$this->holidayModel->where(['id' => $data['id']])->update(['date_describe' => $data['date_describe']])) {
                $this->error('编辑节假日名称失败', null, '', 1);
            }
            $this->success('编辑节假日名称成功', url('Holiday/lists'), '', 1);
        }
    }

    /**同步数据***/
    public function synchrondata()
	{
		$data = input('post.');
		$date_name = $data['date']; // 日期
		$describe = $data['describe'];//节假日名称
		if(get_current_type() != 1){
			$data['company_id'] = get_current_company_id();
		}
		if(!$this->holidayModel->get(['date_name' => $date_name,'company_id'=>$data['company_id']])) {
			if (!$this->holidayModel->save(['date_name' => $date_name, 'date_describe' => $describe,'company_id'=>$data['company_id']])) {
				$this->error('设置节假日失败', url('Holiday/lists'), '', 1);
			}
			$this->success('设置节假日成功', url('Holiday/lists'), '', 1);
		}else{
			$this->error('该日期已设定过', url('Holiday/lists'), '', 1);
		}
    }

    /**
     *  实例化.
     */
    protected function _initialize()
    {
        parent::_initialize();
        $this->holidayModel = new HolidayModel();
        $this->companyModel = new CompanyModel();
        $this->userModel = new UserModel();

    }
}
